package cand

type Cand struct {
	xs [9]bool
}

func New() *Cand {
	return &Cand{}
}

func (c *Cand) Add(i int) {
	c.xs[i-1] = true
}

func (c *Cand) Del(i int) {
	c.xs[i-1] = false
}

func (c *Cand) Has(i int) bool {
	return c.xs[i-1]
}

func (c *Cand) Count() int {
	count := 0
	for _, x := range c.xs {
		if x {
			count++
		}
	}
	return count
}

func (c *Cand) Get() []int {
	results := make([]int, 0, 9)
	for i, x := range c.xs {
		if x {
			results = append(results, i+1)
		}
	}
	return results
}

func (c *Cand) Set(is []int) {
	for _, i := range is {
		c.xs[i-1] = true
	}
}

func Full() *Cand {
	result := New()
	for i := range result.xs {
		result.xs[i] = true
	}
	return result
}

func (c *Cand) Intersect(other *Cand) *Cand {
	result := New()
	for i, x := range c.xs {
		if x && other.xs[i] {
			result.xs[i] = true
		}
	}
	return result
}

func (c *Cand) Union(other *Cand) *Cand {
	result := New()
	for i, x := range c.xs {
		if x || other.xs[i] {
			result.xs[i] = true
		}
	}
	return result
}

func (c *Cand) Diff(other *Cand) *Cand {
	result := New()
	for i, x := range c.xs {
		if x && !other.xs[i] {
			result.xs[i] = true
		}
	}
	return result
}

func (c *Cand) SymDiff(other *Cand) *Cand {
	result := New()
	for i, x := range c.xs {
		if x != other.xs[i] {
			result.xs[i] = true
		}
	}
	return result
}
